package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.dialog.ChooseBankDialog;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ModifyProfileRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ModifyProfileResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import bank.BandCardEditText;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;
/*
* 绑定银行卡
* */

public class BindBankActivity extends BaseActivity implements TextWatcher {

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.card_number)
    BandCardEditText cardNumber;
//    @BindView(R.id.address)
//    EditText address;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.bankname)
    TextView bankname;
    @BindView(R.id.banktype)
    LinearLayout banktype;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank);
        ButterKnife.bind(this);

        init();
    }

    public void setBank(String b){
        bankname.setText(b);
        listenToChange();
    }

    private void init() {
        name.setText(getIntent().getStringExtra("name"));
//        address.addTextChangedListener(this);
        password.addTextChangedListener(this);
        cardNumber.setBankCardListener(new BandCardEditText.BankCardListener() {
            @Override
            public void success(String name) {
                bankname.setText(name);
                listenToChange();
            }

            @Override
            public void failure() {
            }
        });
    }

    @OnClick({R.id.confirm, R.id.banktype})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.banktype:
                ChooseBankDialog chooseBankDialog = new ChooseBankDialog();
                chooseBankDialog.show(getSupportFragmentManager(), null);
                break;
            case R.id.confirm:
                confirm();
                break;
        }
    }

    private void confirm() {
        //sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_withdraw_bankinfo_submit");

        if(bankname.getText().toString().equals("")){
            Utils.showToast("银行类型不能为空");
        } else {
            RetrofitBase.getRetrofit().create(ModifyProfileRequest.class).getResult(UserInfo.getToken(),
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    cardNumber.getText().toString().replaceAll(" ", ""),
                    null,
                    bankname.getText().toString(),
//                    address.getText().toString(),
                    "",
                    null,
                    null,
                    password.getText().toString())
                    .compose(RxHelper.<ModifyProfileResponse>handleResult())
                    .subscribe(new RxSubscribe<ModifyProfileResponse>(this) {
                        @Override
                        protected void _onNext(ModifyProfileResponse modifyProfileResponse) {
                            if (modifyProfileResponse.getStatus() == 1) {
                                Intent intent = new Intent(BindBankActivity.this, WithdrawCashActivity.class);
                                startActivity(intent);

                                finish();
                            }
                        }

                        @Override
                        protected boolean showDialog() {
                            return true;
                        }
                    });
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        listenToChange();
    }

    private void listenToChange() {
//        if (address.getText().length() > 0 && password.getText().length() > 0) {
        if (password.getText().length() > 0) {
            confirm.setEnabled(true);
            confirm.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            confirm.setEnabled(false);
            confirm.setTextColor(Color.parseColor("#7E33CD"));
        }
    }
}